
import UIKit

extension UIViewController
{
    ///推出原生弹框
    func alert(_ title:String?=nil, _ msg:String?=nil, _ leftBlock:(()->Void)?=nil, _ rightBlock:(()->Void)?=nil)
    {
        let alert = UIAlertController( title:LS(title ?? ""), message: LS(msg ?? ""), preferredStyle: .alert)
        let cancel = UIAlertAction(title: LS("取消"), style: .cancel) { action in
            alert.dismiss(animated: true) {
                leftBlock?()
            }
        }
        
        let enter = UIAlertAction(title:LS("确定"), style: .default) { action in
            rightBlock?()
        }
        
        alert.addAction(cancel)
        alert.addAction(enter)
        self.present(alert, animated:true, completion: nil)
    }
    ///推出原生sheet
    func sheet(_ title:String?=nil, _ msg:String?=nil, _ sheetArray:[String]?=nil, _ sheetBlock:((_ index: Int, _ sheetTitle:String)->Void)?=nil)
    {
        self.sheet(title, msg, sheetArray, true, sheetBlock)
    }
    
    ///推出原生sheet
    func sheet(_ title:String?=nil, _ msg:String?=nil, _ sheetArray:[String]?=nil, _ cancel:Bool, _ sheetBlock:((_ index: Int, _ sheetTitle:String)->Void)?=nil)
    {
        let alert = UIAlertController(title:LS(title ?? ""), message: LS(msg ?? ""), preferredStyle: .actionSheet)
        for i in 0..<sheetArray!.count
        {
            let sheettitle = sheetArray![i]
            let action = UIAlertAction(title:LS(sheettitle), style: .default) { a in
                sheetBlock?(i, sheettitle)
            }
            alert.addAction(action)
        }
        if cancel
        {
            let cancel = UIAlertAction(title: LS("取消"), style: .cancel) { a in }
            alert.addAction(cancel)
        }
        self.present(alert, animated: true, completion: nil)
    }
    
    ///
    func push(_ vc:UIViewController?=nil, _ animation:Bool)
    {
        if vc != nil{
            self.navigationController?.pushViewController(vc!, animated: animation)
        }
    }
 
    
    func firstResponder(_ view:UIView)->UIView?{
        if view.isFirstResponder{ return view}
        for subView in view.subviews{
            let responder = self.firstResponder(subView)
            if responder != nil{ return responder}
        }
        return nil
    }
}
